import theme from './color';

let themeConfig;
/**
 * 除 series的配置
 * @param {String} themeType 
 */
function initOptionFn(themeType = 'white') {

    themeConfig = theme[themeType];
    /**
     * 默认配置对象
     */
    let initOption = {
        grid: {
            left: 10,
            right: 20,
            bottom: 40,
            top: 20,
            containLabel: true
        },
        color: themeConfig.color,
        tooltip: {
            trigger: 'axis',
            showDelay: 0,
            axisPointer: {
                type: 'shadow'
            },
            textStyle: {
                align: 'left'
            }
        },
        legend: {
            type: 'scroll',
            show: true,
            left: 'center',
            bottom: 10,
            itemWidth: 10,
            textStyle: {
                color: themeConfig.legendColor
            },
            itemHeight: 10,
            data: [],
            pageTextStyle: {
                color: themeConfig.pageTextStyleColor
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: true, //显示刻度在中间
            nameLocation: 'middle',
            splitLine: {
                show: false,
                lineStyle: {
                    color: themeConfig.splitLineColor,
                    width: 1
                }
            },
            axisLabel: {
                color: themeConfig.labelColor,
                interval: 'auto'
            },
            axisTick: {
                show: false,
                lineStyle: {
                    color: themeConfig.axisTickColor,
                }
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: themeConfig.axisLineColor
                }
            },
            data: ''
        },

        yAxis: {
            name: '',
            type: 'value',
            nameTextStyle: {
                color: themeConfig.nameTextStyleColor
            },
            axisLabel: {
                fontSize: 12,
                color: themeConfig.labelColor
            },
            axisTick: {
                show: false,
                lineStyle: {
                    color: themeConfig.axisTickColor,
                }
            },
            splitLine: {
                lineStyle: {
                    color: themeConfig.splitLineColor,
                    width: 1
                }
            },
            axisLine: {
                show: false,
                lineStyle: {
                    color: themeConfig.axisLineColor
                }
            },
            data: []
        }
    };
    return initOption;
}
// series的单独配置
function initSeriesOptionFn() {
    const option = {
        itemStyle: {
            barBorderRadius: 0,
            color: '',
            borderColor: '#000',
            borderWidth: 0,
            borderType: 'solid',
            barBorderRadius: 0,
            shadowBlur: 0,
            shadowColor: '',
            shadowOffsetX: 0,
            shadowOffsetY: 0,
            opacity: 1,
            color: ''
        },
        label: {
            show: false,
            position: 'inside',
            color: themeConfig.seriesLabelColor,
        },
        showBackground: false,
        backgroundStyle: {
            color: themeConfig.backgroundStyleColor,
            barBorderRadius: 0,
        },
        stack: false,
    };
    return option;
}

/**
 * 
 * @param {Object} data 为数据源，
 * @param {Object} setting 为配置源
 */
export function defaultSetting(data = {}, setting = {}) {

    let initOption = initOptionFn(setting.theme);
    if (!window.echarts) {
        console.log(('请先引入echarts'));
        return;
    }
    if (!Array.isArray(data.series)) {
        console.log('series请输入数组')
        return;
    }
    let xAxisData = [];
    let dataTemp = { ...data };
    dataTemp.series.map(item => {
        for (let i in item) {
            if (data.dimension.indexOf(i) > -1) {
                xAxisData.push(item[i]);
                // delete item[i];
            }
        }
    });
    let xAxis = setting.xAxis || {};
    let yAxis = setting.yAxis || {};
    if (data.unit) {
        initOption.grid.top = 30;
        initOption.yAxis.axisLine.show = true;
    }
    initOption.xAxis.axisTick = Object.assign({}, initOption.xAxis.axisTick, xAxis.axisTick);
    initOption.xAxis.axisLine = Object.assign({}, initOption.xAxis.axisLine, xAxis.axisLine);
    initOption.xAxis.splitLine = Object.assign({}, initOption.xAxis.splitLine, xAxis.splitLine);
    initOption.xAxis.axisLabel = Object.assign({}, initOption.xAxis.axisLabel, xAxis.axisLabel);
    initOption.xAxis.data = xAxisData;
    initOption.color = [...setting.color, ...initOption.color];
    initOption.legend = Object.assign({}, initOption.legend, setting.legend);
    initOption.legend.data = data.legend.data || [];
 
    initOption.grid = Object.assign({}, initOption.grid, setting.grid);
    initOption.xAxis.type = xAxis.type || initOption.xAxis.type;

    initOption.yAxis.type = yAxis.type || initOption.yAxis.type;
    initOption.yAxis.name = data.unit;
    initOption.yAxis.axisLine = Object.assign({}, initOption.yAxis.axisLine, yAxis.axisLine)
    initOption.yAxis.axisTick = Object.assign({}, initOption.yAxis.axisTick, yAxis.axisTick);
    initOption.yAxis.splitLine = Object.assign({}, initOption.yAxis.splitLine, yAxis.splitLine);
    initOption.yAxis.axisLabel = Object.assign({}, initOption.yAxis.axisLabel, yAxis.axisLabel);
    if (yAxis.type == 'category') {
        initOption.yAxis.data = xAxisData;
    }
    return initOption;
}

/**
 * 
 * @param {Object} data 为数据源，
 * @param {Object} setting 为配置源
 */
export function defaultSeries(data = {}, setting = {}) {

    let initOption = initSeriesOptionFn();
    initOption.itemStyle = Object.assign({}, initOption.itemStyle, setting.itemStyle);
    initOption.itemStyle.barBorderRadius = setting.barBorderRadius || initOption.itemStyle.barBorderRadius;

    initOption.showBackground = setting.showBackground || initOption.showBackground;
    initOption.backgroundStyle = setting.backgroundStyle || initOption.backgroundStyle;
    initOption.label = Object.assign({}, initOption.label, setting.label);

    let seriesData = [], obj = {};
    for (let i of data.series) {
        for (let n in i) {
            if (data.legend.data.indexOf(n) > -1) {
                obj[n] ? obj[n].push(i[n]) : (obj[n] = []) && obj[n].push(i[n]);
            }
        }
    }


    for (const [keys, values] of Object.entries(obj)) {
        let barWidth = 'auto';
        if (setting.barWidth && typeof (setting.barWidth) == 'number') {
            barWidth = setting.barWidth;
        } else if (setting.barWidth && Object.keys(setting.barWidth).length > 0) {
            barWidth = setting.barWidth[keys] || 12;
        } else if (data.legend.data.length <= 3) {
            barWidth = 12;
        }
        let itemStyle = {};
        if (setting.linear && Object.keys(setting.linear).indexOf(keys) > -1) {
            itemStyle.color = new echarts.graphic.LinearGradient(0, 1, 0, 0, [{
                offset: 0,
                color: setting.linear[keys][0] || ''
            }, {
                offset: 1,
                color: setting.linear[keys][1] || ''
            }], false)
        } else {
            itemStyle.color = '';
        }
        let stack = false;
        if (setting.stack && Object.keys(setting.stack).indexOf(keys) > -1) {
            stack = setting.stack[keys];
        } else {
            stack = false;
        }
        seriesData.push({
            name: keys,
            data: values,
            // markLine: setting.markLine && setting.markLine.indexOf(keys) > -1 ? defaultSet.markLine : {},
            type: 'bar',
            barWidth: barWidth,
            ...initOption,
            ...itemStyle,
            stack: stack,

        });
    }
    return seriesData
}